﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.SQLite;
using System.Data;

namespace ResourceBlender.DAL {
    public class Command : IDisposable {
        public DbCommand InnerCommand { get; set; }
        public DatabaseType DBType { get; set; }

        public DbParameterCollection Parameters {
            get { return InnerCommand.Parameters; }
        }

        public string CommandText {
            get { return InnerCommand.CommandText; }
            set { InnerCommand.CommandText = value; }
        }

        public DbTransaction Transaction {
            get { return InnerCommand.Transaction; }
            set { InnerCommand.Transaction = value; }
        }

        public Command(DatabaseType type, DbCommand cmd) {
            this.DBType = type;
            this.InnerCommand = cmd;
        }

        public Command(DatabaseType type, DbCommand cmd, DbTransaction trans)
            : this(type, cmd) {
            this.Transaction = trans;
        }

        public void AddParameterWithValue(string name, object value) {
            switch(DBType) {
                case DatabaseType.MySQL:
                    (InnerCommand as MySqlCommand).Parameters.AddWithValue(name, value);
                    break;
                case DatabaseType.SQLServer:
                    (InnerCommand as SqlCommand).Parameters.AddWithValue(name, value);
                    break;
                case DatabaseType.SQLite:
                    (InnerCommand as SQLiteCommand).Parameters.AddWithValue(name, value);
                    break;
            }
        }

        public DbDataReader ExecuteReader() {
            return InnerCommand.ExecuteReader();
        }
        public DbDataReader ExecuteReader(CommandBehavior behaviour) {
            return InnerCommand.ExecuteReader(behaviour);
        }
        /// <summary>
        /// Executes the query and returns the first column of the first row in the result
        /// set returned by the query. All other columns and rows are ignored.
        /// </summary>
        /// <returns>The first column of the first row in the result set.</returns>
        public object ExecuteScalar() {
            return InnerCommand.ExecuteScalar();
        }

        /// <summary>
        /// Executes an SQL statement against a connection object.
        /// </summary>
        /// <returns>The number of rows affected.</returns>
        public int ExecuteNonQuery() {
            return InnerCommand.ExecuteNonQuery();
        }

        #region IDisposable Members
        public void Dispose() {
            InnerCommand.Dispose();
        }
        #endregion
    }
}
